package com.ken.astroboy.quartz;

import java.io.IOException;
import java.text.SimpleDateFormat;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ken.astroboy.model.ScheduledTaskCustomData;
import com.ken.astroboy.util.Exceptions;

public abstract class AbstractJob implements Job{

	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		try{
			JobKey key = context.getJobDetail().getKey();			
			JobDataMap dataMap = context.getMergedJobDataMap();
			ScheduledTaskCustomData customData = (ScheduledTaskCustomData) dataMap.get(key.getName());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");

			logger.info("进入定时任务: " + key + " 当前时间是：" + sdf.format(System.currentTimeMillis()));

			if( customData != null ){
				logger.info("customData.friendReceivers: ", customData.getFriendReceivers().toString());
				logger.info("customData.groupReceivers: ", customData.getGroupReceivers().toString());
				logger.info("customData.imageList: ", customData.getImageList().toString());
				logger.info("customData.textList: ", customData.getTextList().toString());				
			}else{
				logger.info("customData is null");	
			}
			
			doJob(customData);

			logger.info("定时任务完成");

		}catch(Exception e){
			logger.info("出错-定时任务发生异常");
			logger.info(Exceptions.getExceptionDetail(e));
		}		
	}
	
	abstract void doJob(ScheduledTaskCustomData customData) throws IOException;

}
